angular.module('login', [])
    .controller('loginCtrl', ['$scope', 'httpService', '$localStorage', '$rootScope', 'nextPageByRole', 'alertmsg', function($scope, httpService, $localStorage, $rootScope, nextPageByRole, alertmsg) {
        $scope.user = {};
        // 登录
        var firstsubmit = true;
        $scope.islogin = false;
        $scope.login = function(bool, user) {
            if (firstsubmit) {
                if (!bool) {
                    firstsubmit = false;
                    httpService.query('web/sign_in', user, 'post').then(function(res) {
                        firstsubmit = true;
                        if (res.code == 200) {
                            $rootScope.token = res.token;
                            $rootScope.user = res.user;
                            $localStorage.mutian_token = res.token;
                            $localStorage.user = res.user;
                            nextPageByRole.go(res.user.role);
                            window.localStorage.setItem('user', JSON.stringify(res.user));
                        } else {
                            alertmsg(res.msg);
                        }
                    }, function() {
                        firstsubmit = true;
                    });
                } else {
                    $scope.islogin = true;
                }

            } else {
                alertmsg('请不要多次提交！');
            }
        };


    }]);
